window.Muni = window.Muni || {};
Muni.Polyline = Muni.Polyline || {};

//opacity
var defaultPolylineOpt = {
    color: 'FF0000',
    weight: 4,       
    opacity: 1.0,
    path: []
};

var preparePolylineOptions = function(cfg){
    var options = {};
    $.extend(options, defaultPolylineOpt, cfg);
    return options;
};

Muni.Polyline.google = (function(){
    var polylines = [];

    return {
        addPolyline: function(map, cfg){
            var options = preparePolylineOptions(cfg);
            var points = [];
            for(o in options.path){
                var op = o;
                var point = new google.maps.LatLng(options.path[op].latitude, options.path[op].longitude);
                points.push(point);
            };
            var polylineOptions = {
                path: points,
                strokeColor: options.color,
                strokeWeight: options.weight,
                strokeOpacity: options.opacity
            };
            var polyline = new google.maps.Polyline(polylineOptions);
            polyline.setMap(map);
            polylines.push(polyline);
            return polyline;
        },

        getPolylines: function(){
            return polylines;
        },

        removePolyline: function(polyline){
            polyline.setMap(null);
            var idx = polylines.indexOf(polyline);
            if(idx != -1){
                polylines.splice(idx, 1);
            }
        }
    };
})();

Muni.Polyline.seznam = (function(){
    var layer = null;
    var polylines = [];

    var prepareLayer = function(map){
        if(layer == null){
            layer = new SMap.Layer.Geometry();
            map.addLayer(layer);
            layer.enable();
        }

    };
    
    return {
        addPolyline: function(map, cfg){
            prepareLayer(map);   
            var options = preparePolylineOptions(cfg);

            var path = [];
            for(i in options.path){
                var point = SMap.Coords.fromWGS84(options.path[i].latitude, options.path[i].longitude);
                path.push(point);
            }
            var sOptions = {
                color: options.color,
                width: options.weight,
                opacity: options.opacity
            };
            var polyline = new SMap.Geometry(SMap.GEOMETRY_POLYLINE, null, path, sOptions);
            layer.addPolyline(polyline);
            polylines.push(polyline);
            return polyline;
            
        },

        getPolylines: function(){
            return polylines;
        },

        removePolyline: function(pl){
            layer.removePolyline(pl);
            var indOf = polylines.indexOf(pl);
            if(indOf != -1){
                polylines.splice(indOf, 1);
            }
        }
    }
    
})();

Muni.Polyline.bing = (function(){
    var polylines = [];
    var map = null;

    var convertToRGB = function(color){
        var cutHex = (color.charAt(0) == '#') ? color.substring(1, 7) : h;
        var r = parseInt(cutHex.substring(0,2), 16);
        var g = parseInt(cutHex.substring(2, 4), 16);
        var b = parseInt(cutHex.substring(4, 6), 16);
        return [r, g, b];
    };


    return {
        addPolyline: function(bmap, cfg){            
            map = bmap;
            var options = preparePolylineOptions(cfg);
            var path = [];
            for(i in options.path){
                var point = new Microsoft.Maps.Location(options.path[i].latitude, options.path[i].longitude);
                path.push(point);
            }

            var rgb = convertToRGB(options.color);            
            var color = new Microsoft.Maps.Color(255 * options.opacity, rgb[0], rgb[1], rgb[2]);
            var bOptions = {
                strokeColor: color,
                strokeThickness: options.weight,
                visible: true
            };
            var polyline = new Microsoft.Maps.Polyline(path, bOptions);
            map.entities.push(polyline);
            polylines.push(polyline);
            return polyline;            
        },

        getPolylines: function(){
            return polylines;
        },

        removePolyline: function(polyline){
            map.entities.remove(polyline);
            var idx = polylines.indexOf(polyline);
            if(idx != -1){
                polylines.splice(idx, 1);
            }            
        }
        
    }
    
})();
